package com.yxhl.zoume.core.user.ui.helper;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.yxhl.zoume.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by menggod on 16/11/9 .
 * 收支明细 弹窗popupwindow  的包装辅助类
 */
public class InExPopupWindowHelper {


    @BindView(R.id.iv_inex_all)
    ImageView mIvInexAll;
    @BindView(R.id.iv_inex_expenses)
    ImageView mIvInexExpenses;
    @BindView(R.id.iv_inex_income)
    ImageView mIvInexIncome;


    private static final int TYPE_WRAP_CONTENT = 0, TYPE_MATCH_PARENT = 1;
    private IncomeExpensesType mCurrentInexType = IncomeExpensesType.ALL;
    private View mAnchor;
    private View popupView;
    private PopupWindow mPopupWindow;
    private ImageView mSelectView;

    InExChangeListener mInExChangeListener;


    public enum IncomeExpensesType {
        ALL(0), EXPENSES(1),INCOME(2);
        int type;

        IncomeExpensesType(int type) {
            this.type = type;
        }
        public int getType(){
            return type;
        }
    }

    public InExPopupWindowHelper(View view) {
        popupView = view;
        ButterKnife.bind(this, view);
    }

    /**
     * 设置 渠道
     *
     * @param type
     */
    public void setCurrentInexType(IncomeExpensesType type) {
        switch (type) {
            case ALL:
                setTypeSelect(mIvInexAll);
                break;
            case INCOME:
                setTypeSelect(mIvInexIncome);
                break;
            case EXPENSES:
                setTypeSelect(mIvInexExpenses);
                break;
            default:
                break;
        }
    }

    public void initPopupWindow(int type) {
        if (type == TYPE_WRAP_CONTENT) {
            mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else if (type == TYPE_MATCH_PARENT) {
            mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mInExChangeListener.popupWindowClose();
            }
        });
    }

    public void showAsDropDown(View anchor) {
        mAnchor = anchor;
        mPopupWindow.showAsDropDown(mAnchor);
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    @OnClick({R.id.ll_inex_all, R.id.ll_inex_expenses, R.id.il_inex_income})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_inex_all:
                setTypeSelect(mIvInexAll);
                mInExChangeListener.inExTypeChanger(IncomeExpensesType.ALL);
                dismiss();
                break;
            case R.id.ll_inex_expenses:
                setTypeSelect(mIvInexExpenses);
                mInExChangeListener.inExTypeChanger(IncomeExpensesType.EXPENSES);
                dismiss();
                break;
            case R.id.il_inex_income:
                setTypeSelect(mIvInexIncome);
                mInExChangeListener.inExTypeChanger(IncomeExpensesType.INCOME);
                dismiss();
                break;
        }
    }

    public void setTypeSelect(ImageView view) {

        if (null != mSelectView) {
            mSelectView.setSelected(false);
            mSelectView = view;
        } else {
            mSelectView = view;
        }
        mSelectView.setSelected(true);
    }

    public void setOnInExTypeChangeLinstener(InExChangeListener inExChangeListener) {
        mInExChangeListener = inExChangeListener;
    }

    /**
     * 支付状态接口回调
     */
    public interface InExChangeListener {
        void inExTypeChanger(IncomeExpensesType incomeExpensesType);
        void popupWindowClose();
    }

}
